import { Router } from "express";
import { r } from "./utils";
import { userMapper } from './database/database';
import { User } from './database/models/user';

const router = Router()

router.get('/hello', (req, res) => r.success(res, 'hello'))

router.get('/user', async (req, res) => {
	r.success(res, 'ok', await userMapper.find())
})

router.get('/user/:id', async (req, resp) => {
	const id = parseInt(req.params.id)
	const res = await userMapper.findOneBy({id})
	r.success(resp, 'ok', res)
})

router.post('/user', async (req, resp) => {
	const body = req.body
	const res = await userMapper.insert(body)	
	r.success(resp, 'ok', res)
})

router.put('/user', async (req, resp) => {
	const body = req.body 
	const res = await userMapper.save(body)
	r.success(resp, 'ok', res)
})

router.delete('/user/:id', async (req, resp) => {
	const id = parseInt(req.params.id)
	r.success(resp, 'ok', await userMapper.delete(id))
})

export default router;